<?php

// 

/**
 * Google map page renderer. 
 * 
 * @see http://scratch.mit.edu/projects/
 * 
 * @copyright (c) 2011 University of Geneva
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
 * @author Laurent Opprecht
 */
class AssetScratchRenderer extends AssetRenderer
{

    /**
     *
     * @param HttpResource $asset 
     */
    public function accept($asset)
    {
        return $asset->url_match('http://scratch.mit.edu/projects/');
    }

    /**
     *
     * @param HttpResource $asset 
     */
    public function render($asset)
    {
        if (!$this->accept($asset))
        {
            return;
        }

        $project = '';
        $params = $asset->doc()->getElementsByTagName('param');
        for ($i = 0; $i < $params->length; $i++)
        {
            $param = $params->item($i);
            if ($param->getAttribute('name') == 'project')
            {
                $project = $param->getAttribute('value');
            }
        }
        if (empty($project))
        {
            return;
        }
        
        $pos = strpos($project, 'static');
        $project = substr($project, $pos, strlen($project) - $pos);
        $project = 'http://scratch.mit.edu/' . $project;
        
        $size = (int) $asset->config('size');
        $size = (24 <= $size && $size <= 800) ? $size : 300;

        $width = $size;
        $height = $size * 3/4;

        $url = $asset->url();
        $url = explode('?', $url);
        $url = reset($url);

        

        $embed = <<<EOT
        <applet width="$size" height="$height" archive="ScratchApplet.jar" codebase="http://scratch.mit.edu/static/misc/" code="ScratchApplet" style="display:block" id="ProjectApplet">
            <param value="$project" name="project">
            <param value="-Xmx128m" name="java_arguments">
            <h3><a href="http://www.java.com/getjava/">Install Java</a> to view this project right on your browser!</h3>
        </applet>
        
EOT;
        $result = array();
        $result[self::EMBED_SNIPPET] = $embed;
        return $result;
    }

}